package tech.yangzhijie.basic.knowledge.mianshi;

import java.util.concurrent.TimeUnit;

/**
 * volatile 可见性代码验证
 */
public class Test01Volatile {

    public static void main(String[] args) {
        MyData01 myData = new MyData01();

        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "开始执行");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myData.add60();
            System.out.println(Thread.currentThread().getName() + "执行结束 num:" + myData.num);
        },"AAA").start();

        while(myData.num == 0){

        }

        System.out.println(Thread.currentThread().getName() + " 主线程执行完成 num:"+myData.num);
    }
}

class MyData01 {
    volatile int num = 0;

    public void add60(){
        this.num = 60;
    }
}
